//
//  ShuhuiViewController.m
//  盘盈贷1.2
//
//  Created by iOS on 15/3/13.
//  Copyright (c) 2015年 iOS. All rights reserved.
//

#import "ShuhuiViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "Shuhui.h"
#import "ShuhuiTableViewCell.h"
@interface ShuhuiViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)UISegmentedControl *segment;
@property (nonatomic, retain)NSString *str;
@property (nonatomic, retain)NSMutableArray *array1;
@end

@implementation ShuhuiViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.title = @"赎回查询";
        
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self LoadingData];
    self.segment = [[UISegmentedControl alloc] initWithItems:@[@"成功赎回", @"赎回历史"]];
    //self.segment.segmentedControlStyle = UISegmentedControlSegmentAlone;
    self.segment.tintColor = [UIColor lightGrayColor];
    self.segment.frame = CGRectMake(0, 65, self.view.frame.size.width, 40);
    [self.segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segment];
    [self.segment release];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 108, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 120;
    [self.view addSubview:self.tableView];
   
    [self.tableView release];


}
- (void)segmentAction:(UISegmentedControl *)segment
{
    switch (_segment.selectedSegmentIndex) {
        case 0:
            self.str =@"http://www.cloudpeng.com:8102/account/myredeemsuccess?strusername=chenying";
            self.array = [NSMutableArray array];
            [self LoadingData];
            break;
        case 1:
           self.str =@"http://www.cloudpeng.com:8102/account/myredeemlist?strusername=chenying";
            self.array1 = [NSMutableArray array];
            [self LoadingData];
            break;
       
    }
}
- (void)LoadingData
{
    if (self.str == nil) {
        self.str =@"http://www.cloudpeng.com:8102/account/myredeemsuccess?strusername=chenying";
//        self.segment.selectedSegmentIndex = 0;
    }
   
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:self.str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        for (NSDictionary *dic in responseObject) {
            Shuhui *model = [[Shuhui alloc] initWithDictionary:dic];
            [self.array addObject:model];
            [self.array1 addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"___________%@",error);
    }];
    
    [self.tableView reloadData];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (self.segment.selectedSegmentIndex == 0) {
        return [self.array count];
    }else {
        return [self.array1 count];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShuhuiTableViewCell *cell = [[ShuhuiTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        
    }
    cell.nameTitle.text = [[self.array objectAtIndex:indexPath.row] Title];
    cell.shuhuizonge1.text = [NSString stringWithFormat:@"%@", [[self.array objectAtIndex:indexPath.row] Amount]];
    cell.shuhuilixi1.text = [NSString stringWithFormat:@"%@", [[self.array objectAtIndex:indexPath.row] BidAmount]];
    cell.jixitime1.text = [[self.array objectAtIndex:indexPath.row] BeginRateTime];
    cell.touzijine1.text = [NSString stringWithFormat:@"%@", [[self.array objectAtIndex:indexPath.row] Principal]];
    cell.lilv1.text = [NSString stringWithFormat:@"%@", [[self.array objectAtIndex:indexPath.row] LoanRate]];
    cell.shuhuitime1.text = [[self.array objectAtIndex:indexPath.row] Deadline];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
